# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
_luaversions="5.1 5.2 5.3 5.4"
_drivers="mysql postgres sqlite3 odbc"

pkgname=lua-sql
_pkgname=luasql
pkgver=2.6.0
pkgrel=1
pkgdesc="Lua SQL drivers"
url="https://keplerproject.github.io/luasql/"
arch="all"
license="MIT"
makedepends="mariadb-connector-c-dev libpq-dev sqlite-dev unixodbc-dev"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev"
	for _d in $_drivers; do
		subpackages="$subpackages lua$_v-sql-$_d:_subpackage"
	done
done
for _d in $_drivers; do
	subpackages="$subpackages lua-sql-$_d:_meta_subpackage"
done

source="$_pkgname-$pkgver.tar.gz::https://github.com/keplerproject/$_pkgname/archive/$pkgver.tar.gz"
builddir="$srcdir/$_pkgname-$pkgver"

prepare() {
	default_prepare

	local lver; for lver in $_luaversions; do
		cp -r "$builddir" "$builddir-$lver"
	done
}

build() {
	local cflags lver

	for lver in $_luaversions; do
		CFLAGS="$CFLAGS $(pkg-config lua$lver --cflags) -fPIC"

		msg "Building for Lua $lver..."
		cd "$builddir-$lver"

		make CFLAGS="$CFLAGS" DRIVER_LIBS="-lmysqlclient" \
			mysql

		make CFLAGS="$CFLAGS" DRIVER_LIBS="$(pkg-config --libs libpq)" \
			postgres

		make CFLAGS="$CFLAGS" DRIVER_LIBS="$(pkg-config --libs sqlite3)" \
			sqlite3

		make CFLAGS="$CFLAGS -DUNIXODBC" DRIVER_LIBS="-lodbc" \
			odbc
	done
}

package() {
	mkdir -p "$pkgdir"
}

_meta_subpackage() {
	local driver="${subpkgname##*-}"
	pkgdesc="$pkgdesc for $driver"

	mkdir -p "$subpkgdir"
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	local driver="${subpkgname##*-}"
	pkgdesc="Lua $lver SQL driver for $driver"
	depends="lua$lver"
	install_if="lua-sql-$driver=$pkgver-r$pkgrel lua$lver"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/$_pkgname/$pkgver-1"

	cd "$builddir-$lver"
	local install_cmod="$(pkg-config --variable INSTALL_CMOD lua$lver)"
	install -D src/$driver.so \
		"$subpkgdir"/$install_cmod/luasql/$driver.so

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="051438d4f5a9059c2ecf52143a06279469c220117b8b99363ea4fda434f08780794f0c4d54185ea891845238b3f5a307ba06ee7a1e7f4583a3b393c63eed66e9  luasql-2.6.0.tar.gz"
